const baseUrl = 'https://zhongyi.jiayixunfang.cn'
// const baseUrl = '/api'

//封装post请求
function post(url, data) {
    return new Promise((resolve, reject) => {
        let token = uni.getStorageSync('token')
        uni.getNetworkType({
            success: res => {
                if (res.networkType == 'none') {
                    uni.showToast({
                        title: '无网络连接，请检查网络后再试！',
                        icon: 'none'
                    })
                } else {
                    if (token != '') {
                        uni.request({
                            url: baseUrl + url,
                            method: 'POST',
                            data,
                            header: {
                                token: token
                            },
                            success: res => {
                                if (res.data.code == 200) {
                                    resolve(res)
                                } else if (res.data.code == 401) {} else {
                                    resolve(res)
                                }
                            },
                            fail: res => {}
                        })
                    } else {
                        uni.request({
                            url: baseUrl + url,
                            method: 'POST',
                            data,
                            success: res => {
                                if (res.data.code == 200) {
                                    resolve(res)
                                } else if (res.data.code == 401) {} else {
                                    resolve(res)
                                }
                            },
                            fail: res => {}
                        })
                    }
                }
            },
            fail: res => {
                uni.showToast({
                    title: '无法获取网络状态，请检查网络！',
                    icon: 'none'
                })
            }
        })
    })
}



function uploadImg(data) {
    return new Promise((resolve, reject) => {
        uni.getNetworkType({
            success: res => {
                if (res.networkType == 'none') {
                    uni.showToast({
                        title: '无网络连接，请检查网络后再试！',
                        icon: 'none'
                    })
                } else {
                    uni.uploadFile({
                        url: baseUrl + '/user/uploadfile',
                        filePath: data,
                        fileType: 'image',
                        name: 'file',
                        success: res => {
                            res.data = JSON.parse(res.data)
                            if (res.data.code == 200) {
                                resolve(res.data)
                            } else {
                                resolve(res)
                            }
                        },
                        fail: res => {
                            console.log(res)
                            uni.showModal({
                                title: '上传失败',
                                content: JSON.stringify(res)
                            })
                        }
                    })
                }
            },
            fail: res => {
                uni.showToast({
                    title: '无法获取网络状态，请检查网络！',
                    icon: 'none'
                })
            }
        })
    })
}

function uploadSound(data) {
    return new Promise((resolve, reject) => {
        uni.getNetworkType({
            success: res => {
                if (res.networkType == 'none') {
                    uni.showToast({
                        title: '无网络连接，请检查网络后再试！',
                        icon: 'none'
                    })
                } else {
                    uni.uploadFile({
                        url: baseUrl + '/user/uploadfile',
                        filePath: data,
                        fileType: 'video',
                        name: 'file',
                        success: res => {
                            res.data = JSON.parse(res.data)
                            if (res.data.code == 200) {
                                resolve(res.data)
                            } else {
                                resolve(res)
                            }
                        },
                        fail: res => {
                            console.log(res)
                            uni.showModal({
                                title: '上传失败',
                                content: JSON.stringify(res)
                            })
                        }
                    })
                }
            },
            fail: res => {
                uni.showToast({
                    title: '无法获取网络状态，请检查网络！',
                    icon: 'none'
                })
            }
        })
    })
}

function uploadVideo(data) {
    return new Promise((resolve, reject) => {
        let token = uni.getStorageSync('token')
        uni.getNetworkType({
            success: res => {
                if (res.networkType == 'none') {
                    uni.showToast({
                        title: '无网络连接，请检查网络后再试！',
                        icon: 'none'
                    })
                } else {
                    uni.uploadFile({
                        url: baseUrl + 'user/uploadvideo',
                        filePath: data,
                        fileType: 'video',
                        name: 'file',
                        token: token,
                        success: res => {
                            res.data = JSON.parse(res.data)
                            if (res.data.code == 200) {
                                resolve(res.data)
                            } else {
                                resolve(res)
                            }
                        },
                        fail: res => {
                            console.log(res)
                            uni.showToast({
                                title: '上传失败',
                                icon: 'none'
                            })
                        }
                    })
                }
            },
            fail: res => {
                uni.showToast({
                    title: '无法获取网络状态，请检查网络！',
                    icon: 'none'
                })
            }
        })
    })
}

//封装get请求
function get(url, data) {
    return new Promise((resolve, reject) => {
        let token = uni.getStorageSync('token')
        uni.getNetworkType({
            success: res => {
                if (res.networkType == 'none') {
                    uni.showToast({
                        title: '无网络连接，请检查网络后再试！',
                        icon: 'none'
                    })
                } else {
                    uni.request({
                        url: baseUrl + url,
                        method: 'GET',
                        data,
                        header: {
                            token: token,
                        },
                        success: res => {
                            if (res.data.code == 200) {
                                resolve(res)
                            } else if (res.data.code == 401) {} else {
                                resolve(res)
                            }
                        },
                        fail: res => {}
                    })
                }
            },
            fail: res => {
                uni.showToast({
                    title: '无法获取网络状态，请检查网络！',
                    icon: 'none'
                })
            }
        })
    })
}

export default {
    post,
    uploadImg,
    uploadVideo,
    get,
    uploadSound
}